package com.gopay.common.user.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseReadDAOImpl;
import com.gopay.common.cipher.utils.SensitiveInfoUtils;
import com.gopay.common.constants.cust.CustPersonInfoConstants;
import com.gopay.common.domain.gopay.GopayAllCustInfo;
import com.gopay.common.user.dao.AllCustInfoQueryDAO;

/**
 * 所有客户信息查询Dao
 * 
 * @AllCustInfoQueryDAOImpl.java
 * @author fanghw
 * @2013-6-18 上午10:51:59 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("allCustInfoQueryDAO")
public class AllCustInfoQueryDAOImpl extends BaseReadDAOImpl<GopayAllCustInfo, String> implements AllCustInfoQueryDAO {

    /**
     * 是否通过实名认证
     * 
     * @param custId
     * @return
     */
    @Override
    public boolean isRealNameCertify(String custId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GopayAllCustInfo.class);
        criteria.add(Restrictions.eq("custId", custId));

        List<GopayAllCustInfo> custList = this.findByCriteria(criteria);
        if (custList != null && custList.size() == 1 && "1".equals(custList.get(0).getIsRealNameCertify())) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public GopayAllCustInfo getAllCustInfoByCustId(String custId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GopayAllCustInfo.class);
        criteria.add(Restrictions.eq("custId", custId));
        List<GopayAllCustInfo> custList = this.findByCriteria(criteria);
        return custList.get(0);
    }

	@Override
	public GopayAllCustInfo findByEmail(String email) {
		DetachedCriteria criteria = DetachedCriteria.forClass(GopayAllCustInfo.class);
        criteria.add(Restrictions.eq("email",email).ignoreCase());
        criteria.add(Restrictions.ne("custStat", "01"));//排除未激活用户
        List<GopayAllCustInfo> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
    }

	@Override
	public GopayAllCustInfo findByCustMob(String mobile) {
		DetachedCriteria criteria = DetachedCriteria.forClass(GopayAllCustInfo.class);
//        criteria.add(Restrictions.eq("mob",mobile));
		 // start pci modify by ssj 2017-04-11  加密  考虑到后期可能会有多个秘钥体系采用in进行查询
        criteria.add(Restrictions.in("mob", SensitiveInfoUtils.getAllHisCipherText(mobile).toArray()));
        //end
        criteria.add(Restrictions.ne("custStat", "01"));//排除未激活用户
        List<GopayAllCustInfo> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
    }
    
    @Override
    public boolean isCustCorpMobile(String mobile) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GopayAllCustInfo.class);
//        criteria.add(Restrictions.eq("mob",mobile));
        // start pci modify by ssj 2017-04-11  加密  考虑到后期可能会有多个秘钥体系采用in进行查询
        criteria.add(Restrictions.in("mob", SensitiveInfoUtils.getAllHisCipherText(mobile).toArray()));
        //end
        criteria.add(Restrictions.eq("custType","2"));
        
        List<GopayAllCustInfo> custList = this.findByCriteria(criteria);
        
        if (custList != null && custList.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean checkEmailExists(String email) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GopayAllCustInfo.class);
        criteria.add(Restrictions.eq("email",email).ignoreCase());
        criteria.add(Restrictions.ne("custStat", CustPersonInfoConstants.CUST_STAT_WAIT_EMAIL_CERTIFY));//排除未激活用户
        return this.findCountByCriteria(criteria) > 0;
    }

    @Override
    public boolean checkMobileNoExists(String MobileNo) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GopayAllCustInfo.class);
//        criteria.add(Restrictions.eq("mob", MobileNo));
        // start pci modify by ssj 2017-04-11  加密  考虑到后期可能会有多个秘钥体系采用in进行查询
        criteria.add(Restrictions.in("mob", SensitiveInfoUtils.getAllHisCipherText(MobileNo).toArray()));
        //end
        criteria.add(Restrictions.ne("custStat", CustPersonInfoConstants.CUST_STAT_WAIT_EMAIL_CERTIFY));//排除未激活用户
        return this.findCountByCriteria(criteria) > 0;
    }

}
